import requests
from pprint import pprint
import csv

f= open('股票.csv' , mode='w', encoding='utf-8' , newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
        '股票代码',
        '股票名称',
        '当前价',
        '涨跌额',
        '涨跌幅',
        '年至至今',
        '成交量',
        '成交额',
        '换手率',
        '市盈率(TTM)',
        '股息率',
        '市值',
])
csv_writer.writeheader()

headers = {
'Cookie':
'cookiesu=291739022063454; device_id=c028eb07472eeafc2fba3629c78492cc; Hm_lvt_1db88642e346389874251b5a1eded6e3=1739022064; HMACCOUNT=B0E4EA13FFDF3831; remember=1; xq_a_token=545fc64fe1103ea8c69cf62fee6df8cd3b16efee; xqat=545fc64fe1103ea8c69cf62fee6df8cd3b16efee; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjM5ODE5Nzc2NDYsImlzcyI6InVjIiwiZXhwIjoxNzQxNjE0MTA1LCJjdG0iOjE3MzkwMjIxMDUzODIsImNpZCI6ImQ5ZDBuNEFadXAifQ.ZQj7Sm-o44mfAtPA9X1HbFnemWo7C5RTdXdz7mFztXQQAG8HgQXPtc1gbdnuL7kxR0opuOKvVX_JhWh_3tgGTuhjaV9eifx1-QJW4h6lyO3JZooMZ8eotFecQLto-RgMFCGtKFrGwNG4Y0dxEh-ucP-doBat7aEOuahsmeM38qyHH4TB5L30h2JqZrtMJY3p0VnmvHaWbRMILENexy0m6mTEUbbQSrPp5Y1cSaRaVUWrgvc-zWQ2OKssyNAmJVxREGVTqP-563Ie84v25y3d2Ud8w9tn7-pjHBSHCTyWD6JwB3IgorrMVfHbhnkKcXuD1Z9giQGZlU8KqelEzTtZZA; xq_r_token=16e2bf015e6949005a1cc17f574a60bc558bcb47; xq_is_login=1; u=3981977646; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1739022144; ssxmod_itna=eqmx9DBii=0=9DDq0d40pApQ5=0QDgeriQDiTAv2m2RSDDsqdTDSxGKidDqxBnWtTYAi7n+YdFDMA5tIQPxIAAvliDFwKIGPpRGoDU4i8DCuSeeTDeWND5xGoDPxDeDADYoRDAqiOD7qDFIdX/lLtDm4GWKeDgeqGgiCrD0+Xg8wrD4qDBUrQDKMrM+DDl+BnYDr=TDOrDYbQKgMTZl0cxK7tDjMrD/8hzw7U=ZPBYenak2=r78qGyCKGuluRl/n4MYlXeL7d4QO4+bTExl7DeDx3aDVGYi0DN/AAoQNe4WgCXiww0xD3o/+eBGDD===; ssxmod_itna2=eqmx9DBii=0=9DDq0d40pApQ5=0QDgeriQDiTAv2m2+xikXGKDloPO4j+HWTtYwxYjgoyGxMK7wite/liGK4wzzK6B4IYKFCGdwiAEkDYioE60YxL9R6YRW4uAxAPmnoexExZcdmd+1+4XC7+jA73iTB35Snom9CC5NlL2w+YiH+AUeVEKMDejKfW2=6MtmGft58jexZLpKsBalQ0DdZYcAn2rmI2xkKB9OupKilxhkacepOMmC7LYIChIk5AYaaFgA/wXH6Kx1kwDT4QlfygQj1kWi=2emmAlakC3a2k3SlBrpAqxz=0TAnE341Dx9Kno3A5jyxLh0jIL3IiQTOzaxUPuzpo7+ORIpRhxQx8fyL378I5TAT8Wc17OW4ehG5v8atIy3LO2lHwi71RHNpehUHvIHGbc+lgtzbM3HTbqcAw6QukQRQ9ex/vj4W2mTxV0PaiwBBKuveAPbRnv3RwKWKEpnlW1l2F5n8jgk5K67iYNdb7RZnx1EobQxYAgFqwwS+ZSH5nrFtw/Fi8ehkU8ADRqLjw7drBpCzcIPRyWO5QEOjudEhNeSYrMOIMOYqYkoLVMa4cRBgmjrQOEbRHvgav1E=a+3rgI37vaOregdD077cPwRj4d=e7sP4xWN7GxM8yDW+5=O2TudhoHpyYh6xq2jp255xxzetDMYVB7Kh+=ESx8KK4OBq1acgdKiDfs=GbDs5n/=qPbojoBHOnPm20exD08DG7rGDD===',

'User-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0'



}

for page in range (0,3):
    url = f'https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page={page}&size=30&order=desc' \
          f'&order_by=percent&market=CN&type=sh_sz&md5__1632=eqfxyDBD9DgDuQitDsD7IqwwaxRlafDArWxTD'
    response = requests.get(url=url, headers=headers)
    json_data = response.json()

    for i in json_data['data']['list']:
        dit = {
            '股票代码': i['symbol'],
            '股票名称': i['name'],
            '当前价': i['current'],
            '涨跌额': i['chg'],
            '涨跌幅': i['percent'],
            '年至至今': i['current_year_percent'],
            '成交量': i['volume'],
            '成交额': i['amount'],
            '换手率': i['turnover_rate'],
            '市盈率(TTM)': i['pe_ttm'],
            '股息率': i['dividend_yield'],
            '市值': i['market_capital'],
        }
        csv_writer.writerow(dit)
        print(dit)




